package org.adw.launcher2.ld.secret.ui.adapter;

import java.util.LinkedList;
import java.util.List;

import org.adw.launcher2.ld.secret.dom.MyVideo;
import org.adw.launcher2.ld.secret.ui.utils.VideoImageLoader;

import org.adw.launcher2.R;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class MyVideoAdapter extends ArrayAdapter<MyVideo> {

	private Context context;
	private LayoutInflater inflater;
	public static List<MyVideo> listToHide = null;
	public List<MyVideo> listToThread = null;
	public VideoImageLoader imageLoader; 
	public MyVideoAdapter(Context context, int textViewResourceId,
			List<MyVideo> list) {
		super(context, textViewResourceId, list);
		this.context = context;
		listToThread = list;
		inflater = LayoutInflater.from(context);
		listToHide = new LinkedList<MyVideo>();
		
	}
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
//		if(convertView==null){
			convertView = inflater.inflate(R.layout.my_video_item, null);
			holder = new ViewHolder();
			holder.videoThumb = (ImageView) convertView.findViewById(R.id.imgVideo);
			holder.videoName = (TextView) convertView.findViewById(R.id.txtVideoName);
			holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBoxVideo);
			convertView.setTag(holder);
//		}else{
//			holder = (ViewHolder) convertView.getTag();
//		}
			
		final MyVideo myVideo = getItem(position);
		String [] temp = myVideo.getAbsolutePathNoExtend().split("/");
		holder.videoName.setText(temp[temp.length-1]);
		if(myVideo.image==null){
			imageLoader = new VideoImageLoader(this.context,myVideo, holder.videoThumb);
			imageLoader.execute();
		}else{
			holder.videoThumb.setImageBitmap(myVideo.image);
		}
		holder.checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					if(!listToHide.contains(myVideo)){
						listToHide.add(myVideo);
					}
				}else{
					listToHide.remove(myVideo);
				}

			}
		});
		if(this.context.toString().contains("MyNonHiddenVideoActivity")){
			convertView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					showDialog(myVideo.getId());
				}
			});
		}

		return convertView;
		
	}
	
	protected void showDialog(final long videoId) {
		final CharSequence[] items = {"View this video"};

		AlertDialog.Builder builder = new AlertDialog.Builder(this.context);
		Log.d("this.context", this.context.toString());
		builder.setTitle("Choose an action");
		builder.setItems(items, new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int item) {
				Intent intent = new Intent
				(Intent.ACTION_VIEW, Uri.withAppendedPath(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, ""+videoId));
				context.startActivity(intent);
		    }
		});	
		AlertDialog alert = builder.create();
		alert.show();
	}

	static class ViewHolder {
		ImageView videoThumb;
		TextView videoName;
		CheckBox checkBox;
		
	}
}
